
cap program drop fowardlog_construction
program fowardlog_construction
*********************** Created on 12 Aug 22 *************************************************
* This program needs the following 5 variables to be exactly the same name in the data: 
	*1) firm_id; 
	*2) bid_date; 
	*3) winbid; the amount the winning bidder receives. 
		* Specify a variable name as the FIRST argument if you use a customized variable name (added on 11may22);
	*4) winner; assigns 1 for the winner and 0 for others. 
		* Specify a variable name as the SECOND argument if you use a customized variable name (added on 11may22);
	*5) contract_id; numeric
*Note*
*1 Exceptions are 3) and 4). The code accepts a customized varname by specifing the name in the argument
* 
*2 FLAG; the codes ignores observations with FLAG == 1
********************************************************************************************

args arg_winbid arg_winner
global days "30 60 90 120 180 365"

local winbid = "winbid"
if "`arg_winbid'" != ""{
	local winbid = "`arg_winbid'"
}
local winner = "winner"
if "`arg_winner'" != ""{
    local winner = "`arg_winner'"
}



foreach v in "firm_id" "`winbid'" "bid_date" "`winner'" "contract_id"{
	*cap noisily di "`v'"
	cap sum `v' 
	if _rc != 0{
		di as error "`v' is missing" _continue
		di as text""
	}
}
cap gen FLAG = 0

/* create flogx capx utilx*/
foreach x in $days {

	set seed 1281283
	cap drop flog`x'
	gen double flog_temp = `winner' * (1-FLAG) * `winbid'
	gen double fcum_temp = `winner' * (1-FLAG) 
	replace flog_temp = 0 if flog_temp==. 
	replace fcum_temp = 0 if fcum_temp==.

	expand 2 if `winner' == 1 & FLAG == 0, gen(expand)
	replace flog_temp = -flog_temp if expand == 1 
	replace fcum_temp = -fcum_temp if expand == 1 
	replace bid_date = bid_date - `x' if expand == 1
	*replace contract_id = - contract_id if expand == 1

 *ソートしたときexpand=1がその日の最初にくるようにする。そうでないと、オークションによっては1年前の案件の償却が反映されない。
	gsort firm_id bid_date  -expand -winner

	by firm_id: replace flog_temp = flog_temp + flog_temp[_n-1] ///
		if firm_id == firm_id[_n-1]
	
	by firm_id: replace fcum_temp = fcum_temp + fcum_temp[_n-1] ///
		if firm_id == firm_id[_n-1]
		
	*replace flog_temp = flog_temp - `winbid' if `winner' == 1 /* 受注時のamountはflog_tempに含めない*/	
	*replace fcum_temp = fcum_temp - 1 if `winner' == 1 
	
	drop if expand == 1
	cap drop temp
	
	*Drop obs. with flog_tempclose to 0 (double)
	sum flog*, d
	*replace flog_temp = 0 if flog_temp < 1 & flog_temp !=.

	by firm_id bid_date: egen flog_x = max(flog_temp)
	replace flog_x = 0 if flog_x > 0
	replace flog_temp = -flog_x
	by firm_id bid_date: egen fcum_win_x = max(fcum_temp)
	replace fcum_temp = -fcum_win_x

	drop flog_x fcum_win_x expand
	

	*drop flog_x fcum_win_x expand
	*もし同日に複数回オークションに勝ったら、オークションによっては同日に勝った別のオークションがバックログに入ってしまう。
	*するとソートのされ方によってUtilが変わる。
	*同日に勝ったオークションはバックログに入れない。
	rename flog_temp flog`x'
	rename fcum_temp fcum_win`x'
	replace flog`x' = 0 if flog`x' < .000001 & flog`x' > 0   /* added on 22jan22*/
	replace flog`x' =. if bid_date ==.    /* added on 22jan22*/ 
	replace fcum_win`x' = 0 if fcum_win`x' < .000001 & fcum_win`x' > 0   /* added on 22jan22*/
	replace fcum_win`x' =. if bid_date ==.    /* added on 22jan22*/ 
	*br firm_id contract_id bid_date winbid winner flog30 fcum_win30  if firm_id == 20 

}
order firm_id `winner' `winbid' flog*

/*
/* create date from winning */	
 sort firm_id bid_date
 * bidder毎に時系列でソートする
 by firm_id: gen ord=_n
 * bidder毎に順番をつける
 gen windate=bid_date if `winner'==1
 * 勝った日をつける
 replace windate=windate[_n-1] if ord>1 & `winner'~=1
 * 勝たなかった日に関しては最後に勝った日付をいれる
 gen date_from_win=bid_date-windate if `winner'~=1
 * 勝たなかった日に関しては最後に勝った日からの経過日数を入れる
 replace date_from_win=bid_date-windate[_n-1] if ord>1 & `winner'==1
 * 勝った日に関しても前回に勝った日からの経過日数を入れる。
 gen dates_after_win=1 if date_from_win~=.
 * 同日に複数回参加したオークションでは、過去に勝ったオークションからの日数に経過日数をそろえる
 by firm_id bid_date: ///
			egen dates_after_win_x = max(dates_after_win)
 drop dates_after_win
 rename dates_after_win_x dates_after_win

/* create capacity : max(flog365) in fy 2001 for each firm */
	/*bys bidder_muni_id: egen basefy = min(fy)
	bysort bidder_muni_id fy: egen ccc = max(flog365) if fy == basefy & fy < .
	bysort bidder_muni_id: egen cap = max(ccc)
	drop ccc*/
	bysort firm_id: egen cap = max(flog365)
	
/* sales / probiwin in the first fiscal year
	gen `winner'01 = 0
	replace `winner'01 = 1 if `winner' == 1
	bysort firm_id fy: egen rev1y = ///
		total( `winner' * (1-FLAG) * win_bid_pct * reserve_n )
	cap drop probwin1y
	bysort FLAG bidder_muni_id fy: egen probwin1y =	mean( winner01 ) ///
		if FLAG == 0
	drop winner01
	sort bidder_muni_id bid_date
	/* fy1y is the first fiscal year in which the firm bids */
	gen fy1y = fy
	replace fy1y = fy1y[_n-1] if bidder_muni_id == bidder_muni_id[_n-1] ///
		& _n > 1
	sort bidder_muni_id bid_date		
	replace rev1y = rev1y[_n-1] if bidder_muni_id == bidder_muni_id[_n-1] ///
		& _n > 1
	replace probwin1y = probwin1y[_n-1] if bidder_muni_id == bidder_muni_id[_n-1] ///
		& _n > 1
	br bidder_muni_id bidder fy bid_date rev1y*/

	
/* create util: flogx / cap and 2003 or later of fiscal year */
foreach x in $days {
	gen util`x' = flog`x' / cap /*if fy > basefy*/
}

/* create utilav: flogx / average forwardlog size in data period*/
foreach x in $days {
	bys firm_id: egen avrflog`x' = mean(flog`x')
	qui gen utilav`x' = flog`x' / avrflog`x'
}

/* create utilnm: (flogx - average forwardlog) / sd_forwardlog*/
foreach x in $days {
	cap drop avrflog`x'
	bys firm_id: egen avrflog`x' = mean(flog`x')
	bys firm_id: egen sdflog`x' = sd(flog`x')
	bys firm_id: egen maxflog`x' = max(flog`x')
	gen utilnm`x' = (flog`x' - avrflog`x')/sdflog`x'
	gen utilnm2_`x' = flog`x'/maxflog`x'
}

foreach x in $days {
	qui gen utildf`x' = flog`x' - avrflog`x'
}

/* create util_an: flogx / flog365*/
foreach x in $days {
	qui gen util_an`x' = flog`x' / flog365
}

/* log forwardlog */
foreach x in $days {
	cap drop lnflog`x'
	qui gen lnflog`x' = log(1 + flog`x')
	label var lnflog`x' "`x'-Days Log fowardlog"
}
/*
*/
*/

/* log forwardlog */
foreach x in $days {
	cap drop lnflog`x'
	qui gen lnflog`x' = log(1 + flog`x')
	label var lnflog`x' "`x'-Days Log forwardlog"
}
*/
/*


*sort firm_code bid_win_date /* as of 14jan22 */
sort firm_id bid_date contract_id /* deadlineforbidsub contract_id added on 16jan22 to fix the order */
qui bysort firm_id bid_date contract_id : gen NN = _N
by firm_id: gen numa = _n if firm_id < .
/* constraint: "if firm_id < ." added on 16jan22*/

foreach x in $days {
	set seed 182273
	cap drop sumflog`x'
	by firm_id: gen sumflog`x' = sum(flog`x') if firm_id < .
	qui gen meanflog`x'=sumflog`x'/numa
	qui gen sqflog`x' = (flog`x')^2
	by firm_id: gen sumsqflog`x' = sum(sqflog`x')
	/*gen sdflog`x' = sqrt((sumsqflog`x' - (meanflog`x')^2)/(numa-1)) as of 20jan22*/
	qui gen sdflog`x' = sqrt((sumsqflog`x' - numa * (meanflog`x')^2)/(numa-1))
	qui gen rflog`x' = (flog`x' - meanflog`x')/sdflog`x' if firm_id < .
	*replace rflog`x' = . if flog`x' == 0 
	*replace rflog`x' = 0 if flog`x' == 0
	label var rflog`x' "`x'-Days Standardized fowardlog"
}

drop NN numa 
*/ 
compress
end
